package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.InteractiveRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface InteractiveRecordRepository extends JpaRepository<InteractiveRecord, String>, JpaSpecificationExecutor {
    InteractiveRecord findOneByNoteIdAndUserIdAndReceiveUserIdAndInteractiveType(String noteId, String userId, String receiveUserId, Integer interactiveType);

    Integer countAllByReceiveUserIdAndIsLookup(String receiveUserId, Boolean lookup);

    Integer countAllByReceiveUserIdAndInteractiveTypeAndIsLookup(String receiveUserId, Integer interactiveType, Boolean lookup);

    Integer countAllByUserIdAndReceiveUserIdAndIsLookup(String userId, String receiveUserId, Boolean lookup);

    Integer countAllByUserIdAndReceiveUserIdAndInteractiveTypeAndIsLookup(String userId, String receiveUserId, Integer interactiveType, Boolean lookup);

    @Query(value = "select count (i) from InteractiveRecord i where i.receiveUserId = :receiveUserId and i.userId <> :userId and i.interactiveType <> :interactiveType and i.isLookup = :lookup")
    Integer countAllInteractionExceptLikes(@Param("receiveUserId") String receiveUserId,
                                           @Param("userId") String userId,
                                           @Param("interactiveType") Integer interactiveType,
                                           @Param("lookup") Boolean lookup);

    @Modifying
    @Query(value = "update InteractiveRecord  i set i.isLookup=true where i.noteId=:noteId ")
    void closeInteractiveRecordByNoteId(@Param("noteId") String noteId);


    @Modifying
    @Query(value = "update InteractiveRecord  i set i.isLookup=true where i.receiveUserId=:userId and i.interactiveType=2 ")
    void clearLikeRedDot(@Param("userId") String userId);

}
